
package com.bear.cmcc.NetTestHTTP.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.GestureDetector.OnGestureListener;
import android.widget.LinearLayout;
import android.widget.ListView;

/**
 * 自定义支持横向滚动的ListView
 */
public class HVListView extends ListView {

    private final String TAG = "HVListView";

    /** 手势 */
    private GestureDetector mGesture;

    /** 列头 */
    private LinearLayout mListHead;

    /** 偏移坐标 */
    private int mOffset = 0;

    private int moveX;

    /** X移动的宽度 */
    private int widthX;

    private boolean isMove;//这个用来判断是否是 横移

    /** 构�?函数 */
    public HVListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGesture = new GestureDetector(context, mOnGesture);
    }

    public LinearLayout getmListHead() {
        return mListHead;
    }

    public void setmListHead(LinearLayout mHead) {
        this.mListHead = mHead;
        mListHead.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        moveX = wm.getDefaultDisplay().getWidth() - mListHead.getMeasuredWidth();
    }

    /** 分发触摸事件 */
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN: {
                //这个用来判断是否是最左边横移动
                if (ev.getRawX() > mListHead.getLeft()) {
                    widthX = mListHead.getLeft() - moveX;
                    isMove = true;
                } else {
                    isMove = false;
                }
            }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        if (!isMove) {
            return super.onTouchEvent(ev);
        } else {

            return mGesture.onTouchEvent(ev);
        }
    }

    public boolean superOnTouchEvent(MotionEvent ev) {
        return super.onTouchEvent(ev);
    }

    /** 手势 */
    private OnGestureListener mOnGesture = new GestureDetector.SimpleOnGestureListener() {

        public boolean onDown(MotionEvent e) {
            Log.d(TAG, "moveX:" + moveX);
            return true;
        }

        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            return false;
        }

        /** 滚动 */
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (isMove) {
                synchronized (HVListView.this) {
                    int moveX = (int) distanceX;
                    int curX = mListHead.getScrollX();
                    int dx = moveX;
                    // 控制越界问题
                    if (curX + moveX < 0)
                        dx = 0;
                    Log.d(TAG, "11111111111");
                    if (moveX > 0 && curX > widthX) {
                        dx = 0;
                        Log.d(TAG, "2222222222222");
                    }

                    mOffset += dx;
                    // 根据手势滚动Item视图
                    for (int i = 0, j = getChildCount(); i < j; i++) {
                        View child = ((ViewGroup) getChildAt(i)).getChildAt(1);
                        if (child.getScrollX() != mOffset)
                            child.scrollTo(mOffset, 0);
                    }
                    mListHead.scrollBy(dx, 0);
                }
                requestLayout();
                Log.d(TAG, "isMove:true");
            } else {
                Log.d(TAG, "isMove:false");

            }
            return true;
        }
    };

    /** 获取列头偏移�? */
    public int getHeadScrollX() {
        return mListHead.getScrollX();
    }

}
